#pragma once
#include <array>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>

namespace U32
{
	using namespace std;
	class statistics
	{
	private:
		array<int, 1000001> salaries;
	public:

		statistics()
		{
			clear();
		}

		~statistics()
		{
		}

		void read_salaries(string filename)
		{
			string row;
			try
			{
				ifstream fin(filename);
				while (getline(fin, row))
				{
					int salary;
					istringstream iss(row);
					iss >> salary;
					if (salary >= 20000 && salary <= 1000000)
						salaries[salary]++;
				}
				fin.close();
			}
			catch (exception e)
			{
				cout << e.what() << endl;
			}
		}

		void clear()
		{
			for (int i = 0; i < salaries.size(); i++)
			{
				salaries[i] = 0;
			}
		}

		int calculate_mean()
		{
			long long sum = 0;
			int num = 0;
			for (int i = 0; i < salaries.size(); i++)
			{
				sum += i * salaries[i];
				num += salaries[i];
			}
			return sum / num;
		}

		int calculate_median()
		{
			int num = 0;
			for (int i = 0; i < salaries.size(); i++)
			{
				num += salaries[i];
			}
			if (num % 2 == 1)
			{
				num = num / 2;
				int i = 0;
				while (num >= 0)
				{
					num -= salaries[i];
					i++;
				}
				return i;
			}
			else
			{
				num = num / 2;
				int i = 0;
				int j = 0;
				while (num >= 0)
				{
					num -= salaries[i];
					j = i;
					i++;
				}
				return (i + j) /2;
			}
		}
	};
}